﻿using Abp;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using AutoMapper.QueryableExtensions;
using FwProject.Shop.Dto;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.Shop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;

namespace FwProject.Shop
{
    public class ShopTagAppService : FwProjectAppServiceBase, IShopTagAppService
    {
        private readonly IRepository<ShopTag, Guid> _repositoryShopTag;
        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }
        public ShopTagAppService(IRepository<ShopTag, Guid> repositoryShopTag)
        {
            _guid = SequentialGuidGenerator.Instance;
            _repositoryShopTag = repositoryShopTag;
        }

        public async Task<LayerDataBaseDto> GetLayerDataAsync(int page, int limit, TagTypeEnum? tagType)
        {
            var query = _repositoryShopTag.GetAll();

            if (tagType.HasValue)
            {
                query = query.Where(o => o.TagType == tagType.Value && o.IsActive);
            }
            var pagelist = await query.OrderByDescending(o => o.CreationTime).ProjectTo<ShopTagDto>().ToPagedListAsync(page, limit);

            LayerDataBaseDto dto = new LayerDataBaseDto();
            dto.Data = pagelist;
            dto.Count = pagelist.TotalItemCount;

            return dto;
        }

        public async Task<Guid> CreateAsync(ShopTagInput input)
        {
            var entity = input.MapTo<ShopTag>();
            //var model = _map.Map<ShopTag>(input);
            entity.Id = _guid.Create();
            return await _repositoryShopTag.InsertAndGetIdAsync(entity);
        }

        public async Task ModifyAsync(Guid id, ShopTagInput input)
        {
            var entity = await _repositoryShopTag.GetAsync(id);
            input.MapTo(entity);
            await _repositoryShopTag.UpdateAsync(entity);

        }

        public async Task DeleteAsync(Guid id)
        {
            await _repositoryShopTag.DeleteAsync(id);
        }

        public async Task<ShopTagInput> GetEnterDtoAsync(Guid id)
        {
            var entity = await _repositoryShopTag.GetAsync(id);
            var dto = entity.MapTo<ShopTagInput>();
            return dto;
        }

        public async Task SwitchActive(Guid id, bool active)
        {
            var entity = await _repositoryShopTag.GetAsync(id);
            entity.IsActive = active;
            await _repositoryShopTag.UpdateAsync(entity);
        }
        /// <summary>
        /// app获取常用的标签查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<IPagedList<ShopTagDto>> GetAppShopTag(int page,int pagesize) {


           return await _repositoryShopTag.GetAll().Where(o => o.IsActive == true && o.TagType == TagTypeEnum.商品).ProjectTo<ShopTagDto>().ToPagedListAsync(page, pagesize);
        }
    }
}
